package com.jeewms.www.wms.ui.adapter;

import android.annotation.SuppressLint;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ma on 2015/7/10.
 * RecyclerView通用数据绑定
 */
  public class RecyclerViewBindingAdapter<T> extends RecyclerView.Adapter<RecyclerViewBindingAdapter.BindingHolder> {


    public final int layoutId;
    public final int br;

    public void setData(List<T> data) {
        if (data != null)
            this.data = data;

    }

    public List<T> getData() {

        return data;
    }

    protected List<T> data = new ArrayList<>();

    /**
     * @param br       BR.XXX
     * @param layoutId 布局
     */
    public RecyclerViewBindingAdapter(int br, int layoutId) {

        this.layoutId = layoutId;
        this.br = br;
    }


    public static class BindingHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        public BindingHolder(View itemView) {
            super(itemView);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
                layoutId,
                viewGroup,
                false);



        BindingHolder holder = new BindingHolder(binding.getRoot());
        holder.setBinding(binding);

        return holder;
    }

    //define interface
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener = null;

    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(BindingHolder holder,  final int position) {

        if (onItemClickListener != null) {
            holder.itemView.setClickable(true);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });

        }
        holder.getBinding().setVariable(br, data.get(position));
        holder.getBinding().executePendingBindings();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public T lastItem() {
        return data.get(data.size() - 1);
    }

    public void add(T t) {
        insert(t, data.size());
    }

    public void insert(T t, int position) {
        data.add(position, t);
        notifyItemInserted(position);
    }

    public void remove(int position) {
        data.remove(position);
        notifyItemRemoved(position);
    }

    public void clear() {
        int size = data.size();
        data.clear();
        notifyItemRangeRemoved(0, size);
    }


    public void addAll(List<T> list) {
        if (list.isEmpty()) {
            return;
        }
        int startIndex = data.size();
        data.addAll(startIndex, list);
        notifyItemRangeInserted(startIndex, list.size());
    }

    public T getItem(int position) {
        if (position >= data.size()) return null;
        return data.get(position);
    }


}